package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.Model;

public class ButtonPanel extends JPanel implements ActionListener{
	
	
	private static final long serialVersionUID = 1L;
	UpdatePanel updatePanel;
	JButton addPeerButton;
	JButton addSeedButton;
	JButton addLeechButton;
	RemovePanel removePanel;
	Model model;
	TopPanel parent;
	
	double minDown = 50;
	double maxDown = 1000;
	double minUp = 25;
	double maxUp = 200;
	Random random = new Random();

	public ButtonPanel(Model model, TopPanel parent) {
		this.model = model;
		this.parent = parent;
		setLayout(new BorderLayout());
		updatePanel = new UpdatePanel(model, parent);
		addPeerButton = new JButton("Add Peer");
		addSeedButton = new JButton("Add Seed");
		addLeechButton = new JButton("Add Leech");
		removePanel = new RemovePanel(model, parent);
		
		addPeerButton.addActionListener(this);
		addSeedButton.addActionListener(this);
		addLeechButton.addActionListener(this);
		
		add(updatePanel, BorderLayout.NORTH);
		add(addPeerButton, BorderLayout.CENTER);
		add(addSeedButton, BorderLayout.EAST);
		add(addLeechButton, BorderLayout.WEST);
		add(removePanel, BorderLayout.SOUTH);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==addPeerButton){
			String s = JOptionPane.showInputDialog("Choose Goal Share Ratio", "1.1");
			String d = JOptionPane.showInputDialog("Choose Download Speed", "500.0");
			String u = JOptionPane.showInputDialog("Choose Upload Speed", "250.0");
			String n = JOptionPane.showInputDialog("Choose Number to Make", "1");
			int num = Integer.parseInt(n);
			for(int i=0; i<num; i++){
				model.addNewNode(Double.parseDouble(d), Double.parseDouble(u), false, false, Double.parseDouble(s));
			}
		}
		if(e.getSource()==addSeedButton){
			String d = JOptionPane.showInputDialog("Choose Download Speed", "500.0");
			String u = JOptionPane.showInputDialog("Choose Upload Speed", "250.0");
			String n = JOptionPane.showInputDialog("Choose Number to Make", "1");
			int num = Integer.parseInt(n);
			for(int i=0; i<num; i++){
				model.addNewNode(Double.parseDouble(d), Double.parseDouble(u), false, true, 100);
			}
		}
		if(e.getSource()==addLeechButton){
			String d = JOptionPane.showInputDialog("Choose Download Speed", "500.0");
			String u = JOptionPane.showInputDialog("Choose Upload Speed", "250.0");
			String n = JOptionPane.showInputDialog("Choose Number to Make", "1");
			int num = Integer.parseInt(n);
			for(int i=0; i<num; i++){
				model.addNewNode(Double.parseDouble(d), Double.parseDouble(u), true, false, 100);
			}
		}		
	}
	
	private double getDown(){
		return random.nextDouble()*(maxDown-minDown) + minDown;
	}
	
	private double getUp(){
		return random.nextDouble()*(maxUp-minUp) + minUp;
	}
}
